using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FytSoa.Domain.Core.Result;
using FytSoa.Generator;
using FytSoa.Quartz;
using Microsoft.AspNetCore.Mvc;
using Quartz;
using FytSoa.Common.Utils;
using Newtonsoft.Json;
using FytSoa.Common.Captcha;

namespace FytSoa.ApiService.Controllers
{
    /// <summary>
    /// 代码生成
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class GeneratorController : ControllerBase
    {
        

        [HttpGet]
        public ApiResult<string> Get([FromQuery]GeneratorTableDto param)
        {
            var gs = new GeneratorService();
            return  gs.CreateCode(param);
        }
        
        [HttpGet]
        [Route("quartz")]
        public async Task<IActionResult> QuartzTest(int type)
        {
            var str = new List<string>();
            var jobKey = new JobKey("demo","taskGroup");
            switch (type)
            {
                //添加个http的日志测试
                case 0:
                    await QuartzService.Instance.Add(new SchedulerEntity()
                    {
                        JobName = jobKey.Name,
                        JobGroup = jobKey.Group,
                        JobId=Unique.Id().ToString(),
                        Cron="0/5 * * * * ? ",
                        TriggerType=TriggerTypeEnum.Cron,
                        RequestUrl="http://localhost:5000/Generator/quartz/http/test",
                        RequestType=RequestTypeEnum.Get
                    });
                    break;
                //添加任务
                case 1:
                    var trigger = TriggerBuilder.Create()
                        .WithDescription(jobKey.Group+"_"+jobKey.Name)
                        .WithIdentity(jobKey.Name)
                        //.WithSchedule(CronScheduleBuilder.CronSchedule("0 0/30 * * * ? *").WithMisfireHandlingInstructionDoNothing())
                        .WithSimpleSchedule(x=>x.WithIntervalInSeconds(5).RepeatForever().WithMisfireHandlingInstructionIgnoreMisfires())
                        .Build();
                    await QuartzService.Instance.Add(typeof(MyJob), jobKey, trigger);
                    break;
                //暂停任务
                case 2:
                    await QuartzService.Instance.TriggerAction(jobKey,JobActionEnum.暂停);
                    break;
                //恢复任务
                case 3:
                    await QuartzService.Instance.TriggerAction(jobKey, JobActionEnum.恢复);
                    break;
                //立刻执行一次
                case 4:
                    await QuartzService.Instance.TriggerAction(jobKey, JobActionEnum.立即执行);
                    break;
                //删除任务
                case 5:
                    await QuartzService.Instance.TriggerAction(jobKey, JobActionEnum.删除);
                    break;
                //查询任务日志
                case 6:
                    str=await QuartzService.Instance.JobLogs(jobKey);
                    break;
                
            }

            return Ok(str.Count==0?"Success":str);
        }

        [HttpGet]
        [Route("quartz/log")]
        public IActionResult QuartzLogs([FromQuery]string name,[FromQuery]string group)
        {
            var jobKey = new JobKey(name, group);
            Logger.Info("进入方法：Logs"+JsonConvert.SerializeObject(jobKey));
            return Ok(QuartzService.Instance.JobLogs(jobKey));
        }
        
        [HttpGet]
        [Route("quartz/http/test")]
        public IActionResult QuartzHttpTest()
        {
            Logger.Info("进入方法：HttpTest",new HttpResultEntity());
            return Ok(new HttpResultEntity());
        }

    }
    
    
    /// <summary>
    /// 测试任务
    /// </summary>
    public class MyJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                Logger.Info("执行MyJob"+DateTime.Now);
            });
        }
    }
}